package se.lnu.axez.widgets;

import se.lnu.axez.BoundingSphere;
import se.lnu.axez.ControlBase;
import se.lnu.axez.FloatQuad;
import se.lnu.axez.TriMesh;
import se.lnu.axez.animation.RotateControlAnimation;
import android.util.Log;

public final class Button extends ControlBase {
	private static final String LOG_TAG = Button.class.getName();
	private static final long ANIMATION_DURATION = 500L;
	private static final float TOUCH_SHIFT = 5f;
	private static final int STATE_DOWN = 1;
	private static final int STATE_UP = 2;
//	private final TranslateControlAnimation deactivateAnimatio;
//	private final TranslateControlAnimation activateAnimation;
	private final RotateControlAnimation touchAnimation;
	private int state = STATE_UP;
	private float idleZ;
	
	public Button(int meshId) {
		super(new ButtonVisual(meshId), BoundingSphere.newInstance()); // TODO: temporary solution, requires box shape
		
		this.touchAnimation = new RotateControlAnimation();
		this.touchAnimation.setDuration(ANIMATION_DURATION);

//		this.activateAnimation = new TranslateControlAnimation();
//		this.activateAnimation.setDuration(ANIMATION_DURATION);
//		
//		this.deactivateAnimation = new TranslateControlAnimation();
//		this.deactivateAnimation.setDuration(ANIMATION_DURATION);
	}
	
	@Override
	protected synchronized void onTouchDown() {
		Log.d(LOG_TAG, "onTouchDown");
		FloatQuad rotation = getTransformation().getRotation();

//		if (!this.touchAnimation.ended()) {
//			return;
//		}

		this.touchAnimation.reset();
		this.touchAnimation.setParams(rotation.angle(), rotation.angle() + 90f, 0f, 1f, 0f);
		addAnimation(this.touchAnimation);
		
//
//		this.idleZ = getTransformation().getTranslation().z();
//		
//		if (this.state == STATE_UP) {
//			this.deactivateAnimation.end();
//
//			FloatTriple pos = getTransformation().getTranslation();
//			this.activateAnimation.setXFrom(pos.x());
//			this.activateAnimation.setXTo(pos.x());
//			this.activateAnimation.setYFrom(pos.y());
//			this.activateAnimation.setYTo(pos.y());
//			this.activateAnimation.setZFrom(pos.z());
//			this.activateAnimation.setZTo(this.idleZ - TOUCH_SHIFT);
//			this.activateAnimation.reset();
//			addAnimation(this.activateAnimation);
//		}
//
//		this.state = STATE_DOWN;
	}

//	@Override
//	protected void onSwipe() {
//		Log.d(LOG_TAG, "onSwipe");
//		
//		if (this.state == STATE_DOWN) {
//			this.animateUp();
//		}
//		
//		this.state = STATE_UP;
//	}
//	
//	@Override
//	protected void onClick() {
//		Log.d(LOG_TAG, "onClick");
//		if (this.state == STATE_DOWN) {
//			this.animateUp();
//		}
//
//		this.state = STATE_UP;
//	}
//
//	private void animateUp() {
//		this.activateAnimation.end();
//
//		FloatTriple pos = getTransformation().getTranslation();
//		this.deactivateAnimation.setXFrom(pos.x());
//		this.deactivateAnimation.setXTo(pos.x());
//		this.deactivateAnimation.setYFrom(pos.y());
//		this.deactivateAnimation.setYTo(pos.y());
//		this.deactivateAnimation.setZFrom(pos.z());
//		this.deactivateAnimation.setZTo(this.idleZ);
//		this.deactivateAnimation.reset();
//		addAnimation(this.deactivateAnimation);
//	}
}
